package com.carl.zlds.controller.admin;

import com.carl.zlds.tool.common.PageBO;
import com.carl.zlds.tool.common.result.Result;
import com.carl.zlds.controller.BaseController;
import com.carl.zlds.pojo.dto.AdSearchDTO;
import com.carl.zlds.pojo.dto.SaveAdDTO;
import com.carl.zlds.pojo.vo.AdVO;
import com.carl.zlds.service.IAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author carl
 */
@Slf4j
@RestController(value = "admin.adController")
@RequestMapping("/admin/1/ad")
@Api(tags = {"管理端 - 广告管理"})
public class AdController extends BaseController {

    private final IAdService adService;

    @Autowired
    public AdController(IAdService adService) {
        this.adService = adService;
    }

    @ApiOperation("保存广告")
    @PostMapping(value = "/save")
    public Result<Void> save(@RequestParam(value = "id", defaultValue = "0") Long id,
                             @Valid SaveAdDTO dto) {
        adService.saveAd(id, dto);
        return Result.success();
    }

    @ApiOperation("删除广告")
    @PostMapping(value = "/delete")
    public Result<Void> delete(@RequestParam("id") Long id) {
        adService.deleteAd(id);
        return Result.success();
    }

    @ApiOperation("广告列表")
    @GetMapping(value = "/list")
    public Result<Map<String, Object>> list(AdSearchDTO dto, PageBO pageBO) {
        Map<String, Object> result = adService.getAdList(dto, pageBO);
        return Result.success(result);
    }

    @ApiOperation("广告详情")
    @GetMapping(value = "/info")
    public Result<AdVO> info(@RequestParam("id") Long id) {
        AdVO adVO = adService.getAdInfo(id);
        return Result.success(adVO);
    }
}
